// pages/wallet/wallet.js
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [
      { id:'1',
        url: '',
        image: '/images/index.jpg',
        text: 'VIP',
        color: 'color:#63B8FF'
      }, {
        id:'2',
        url: '',
        image: '/images/优惠券.png',
        text: '优惠券',
        color: 'color:red'
      }
    ],
    items2: [
      { id:'1',
        url: '',
        image: 'iconfont icon-yinhangqia',
        text: '押金情况',
        color: 'color:#63B8FF'
      }, {
        id:'2',
        url: '',
        image: 'iconfont icon-jiaoyijilu',
        text: '充值（由于没有营业执照所以无法调用微信支付）',
        color: 'color:red'
      }
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that  = this;
    wx.request({
      url: 'https://www.hkzhaoscut.top/getmoney',
      data:{
        u_id:wx.getStorageSync("openid")
      },
      success:function(res){
       that.setData({
         money:res.data
       });
      },
      fail:function(res){
        console.log("余额失败")
      }
    })
    },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.request({
      url: 'https://www.hkzhaoscut.top/getmoney',
      data: {
        u_id: wx.getStorageSync("openid")
      },
      success: function (res) {
        that.setData({
          money: res.data
        });
      },
      fail: function (res) {
        console.log("余额失败")
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  showm:function(e){
    var information;
    var num = e.currentTarget.dataset.id;
    var that = this;
    var vipinformation;
    var vipText;
     if(num==1)
     {
     wx.request({
       url: 'https://www.hkzhaoscut.top/getvip',
       data:{
         u_id:wx.getStorageSync("openid")
       },
       success:function(res){
       information = res.data;
         if (information == '') {
           vipinformation = "你没有开通vip（30/月）";
           vipText = "开通";

         }
         else {
           vipinformation = information;
           vipText = "续费";
         };
         wx.showModal({
           title: 'VIP',
           content: vipinformation,
           confirmText: "确定",
           cancelText: vipText,
           success: function (res) {
             console.log(res);
             if (res.confirm) {
               console.log("我知道了")
             } else {
              if(vipText=="开通")
              {

                wx.redirectTo({
                  url: '../vip/vip?id=2',
                })
                /*wx.request({
                  url: 'https://www.hkzhaoscut.top/newvip',
                  data:{
                    u_id:wx.getStorageSync("openid")
                  },
                  success:function(res){
                    var code = res.statusCode;
                    if(code==200)
                    {wx.showToast({
                      title: '开通成功',
                      icon: 'success',
                      duration: 2000
                    });
                    that.onLoad();
                  }
                    else {
                       wx.redirectTo({
                         url: '../msg_fail/msg_fail',
                       })
                    }},
                  fail:function(){
                    wx.redirectTo({
                      url: '../msg_fail/msg_fail',
                    })
                  }
                })*/
              }
              else{
                wx.redirectTo({
                  url: '../vip/vip?id=2',
                })
                /*wx.request({
                  url: 'https://www.hkzhaoscut.top/plusvip',
                  data:{
                    u_id:wx.getStorageSync("openid")
                  },
                  success:function(res){
                    var f = res.statusCode;
                    if(f==200){
                    wx.showToast({
                      title: '续费成功',
                      icon: 'success',
                      duration: 2000
                    });
                   that.onLoad();
                  }
                  else
                  {
                    wx.redirectTo({
                      url: '../msg_fail/msg_fail',
                    })
                  }
                  },
                  fail: function () {
                    wx.redirectTo({
                      url: '../msg_fail/msg_fail',
                    })
                  }
                })*/
              }
             }
           }
         });
       },
       fail: function (res) {
         console.log("vip fail")
       }
     })
     }
     if(num==2){
       var cinformation;
       wx.request({
         url: 'https://www.hkzhaoscut.top/getcoupon',
         data:{
           u_id:wx.getStorageSync("openid")
         },
         success:function(res)
         {
           cinformation = res.data;
           wx.showModal({
             content: cinformation,
             showCancel: false,
             success: function (res) {
               if (res.confirm) {
                 console.log('用户点击确定')
               }
             }
           });
         },
         fail:function()
         {
           console.log("coupon fail");
         }
       });
     }
  },
  showm2:function(e){
    var n = e.currentTarget.dataset.id;
    var that  =this;
    var ct;
    var contet;
    var bool ;
    if (n== 1) {
         wx.request({
           url: 'https://www.hkzhaoscut.top/getdeposit',
           data:{
             u_id:wx.getStorageSync("openid")
           },
           success:function(res){
             ct = res.data;
             if(ct !='0')
             {  
               bool = "退押金";
               contet = "已缴纳押金 押金金额为200";
             }
             else
             {  bool="交押金";
               contet = "没有缴纳押金，请尽快缴纳";
             }
             wx.showModal({
               title: '押金情况',
               content: contet,
               confirmText: "确定",
               cancelText: bool,
               success: function (res) {
                 console.log(res);
                 if (res.confirm) {
                   console.log("我知道了")
                 } else {
                   //调用一个request加减余额
                   if(bool=="交押金")
                   {
                    
                     wx.request({
                       url: 'https://www.hkzhaoscut.top/paydeposit',
                       data:{
                         u_id:wx.getStorageSync("openid"),
                         amount:200
                       },
                       success:function(res)
                       { var iloveyou = res.statusCode
                         if(iloveyou==200)
                        { wx.showToast({
                           title: '缴纳成功',
                           icon: 'success',
                           duration: 2000
                         });
                         that.onLoad();
                       }
                          else
                          {
                            wx.redirectTo({
                              url: '../msg_fail/msg_fail',
                            })
                          }
                       },
                       fail: function () {
                         wx.redirectTo({
                           url: '../msg_fail/msg_fail',
                         })
                       }
                     })
                   }
                   else
                   {
                     wx.request({
                       url: 'https://www.hkzhaoscut.top/returndeposit',
                       data:{
                         u_id:wx.getStorageSync("openid"),
                       },
                       success:function(res){
                         wx.showToast({
                           title: '退款成功',
                           icon: 'success',
                           duration: 2000
                         });
                         that.onLoad();
                       },
                       fail: function () {
                         wx.redirectTo({
                           url: '../msg_fail/msg_fail',
                         })
                       }
                     })
                   }
                 }
               }
             });
           },
           fail:function(res){
             wx.redirectTo({
               url: '../msg_fail/msg_fail',
             })
           }

         })
     
    }
    if (n == 2) {
     var setUrl = this.data.items2;
     setUrl[1].url =  '../chongzhi/chongzhi';
     this.setData({
       items2:setUrl
     })
     
    }
  }
})